﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class ControlsControl : UserControl, IControlRulable
    {
        private CommandUIDefinitionClasses.Controls innerControls;

        internal CommandUIDefinitionClasses.Controls InnerControls
        {
            get { return innerControls; }
            set { innerControls = value; }
        }

        public ControlsControl(bool isFromCommandUIDefinition)
        {
            InitializeComponent();
            this.innerControls = new CommandUIDefinitionClasses.Controls(isFromCommandUIDefinition);
            this.FillControls();
        }

        public ControlsControl(CommandUIDefinitionClasses.Controls controls)
            :this(true)
        {
            this.innerControls = controls;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.strId.Value = this.innerControls.Id;
                this.rcToolTipDescription.Value = this.innerControls.ToolTipDescription;
                this.strToolTipHelpKeyWord.Value = this.innerControls.ToolTipHelpKeyWord;
                this.rcToolTipImage32by32.Value = this.innerControls.ToolTipImage32by32;
                this.strToolTipImage32by32Class.Value = this.innerControls.ToolTipImage32by32Class;
                this.icToolTipImage32by32Left.Value = this.innerControls.ToolTipImage32by32Left;
                this.icToolTipImage32by32Top.Value = this.innerControls.ToolTipImage32by32Top;
                this.strToolTipShortcutKey.Value = this.innerControls.ToolTipShortcutKey;
                this.rcToolTipTitle.Value = this.innerControls.ToolTipTitle;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerControls.Id = this.strId.Value;
                    this.innerControls.ToolTipDescription = this.rcToolTipDescription.Value;
                    this.innerControls.ToolTipHelpKeyWord = this.strToolTipHelpKeyWord.Value;
                    this.innerControls.ToolTipImage32by32 = this.rcToolTipImage32by32.Value;
                    this.innerControls.ToolTipImage32by32Class = this.strToolTipImage32by32Class.Value;
                    this.innerControls.ToolTipImage32by32Left = this.icToolTipImage32by32Left.Value;
                    this.innerControls.ToolTipImage32by32Top = this.icToolTipImage32by32Top.Value;
                    this.innerControls.ToolTipShortcutKey = this.strToolTipShortcutKey.Value;
                    this.innerControls.ToolTipTitle = this.rcToolTipTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ControlsGroup_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerControls.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
